/**
 * Created by xiangyufang on 2016/10/11.
 */
;(function($,window,document,undefined){
    /***全局变量初始化 start***/
    var $table = $('#JQ_table');
    var DTTABLE;
    var taskPoint;
    var taskInfo;
    /***全局变量初始化 end***/






    //收发球数据
    var taskId = $("#JQ_task_id").val();
    DyyBase.ajax(cleanPipeApi.url_clean_history_record(),{taskId:taskId},function(dataset){
        if(dataset.bool){
            var json = dataset.result;

            //发球点信息；
            drawBeginBall(json.fromPig);
            if(json.fromPigImgs){
                var startImg =  DyyBase.getImagePath(json.fromPigImgs);
                $('#JQ_begin_Img').loadSmallImage(startImg,'','','pdf');
            }else{
                var startImgNull = [];
                $('#JQ_begin_Img').loadSmallImage(startImgNull,'','','pdf');
            }

            //收球点信息；
            drawEndBall(json.toPig);
            if(json.toPigImgs){
                var endImg = DyyBase.getImagePath(json.toPigImgs);
                $("#JQ_end_Img").loadSmallImage(endImg,'','','pdf');
            }else{
                var endImgNull = [];
                $("#JQ_end_Img").loadSmallImage(endImgNull,'','','pdf');
            }

            taskPoint = DyyBase.clearNull(json.taskPoints); //任务点信息
            if(taskPoint){
                drawTable(taskPoint);
            }

            taskInfo = json.pigTask;
        }
    });

    //发球点信息
    function drawBeginBall(data){
        var startBall = DyyBase.clearNull(data);
        var $start_day = $("#JQ_start_day");
        var $start_ball = $("#JQ_start_ball");
        var $start_mile = $("#JQ_start_mile");
        var $start_pile = $("#JQ_start_pile");
        var $start_pileMile = $("#JQ_start_pileMile");
        var $start_time = $("#JQ_start_time");
        var $start_man = $("#JQ_start_man");
        var $start_out = $("#JQ_start_out");
        var $start_in = $("#JQ_start_in");
        var $start_flow = $("#JQ_start_flow");
        var $start_bore = $("#JQ_start_bore");
        var $start_type = $("#JQ_start_type");
        var $start_tool = $("#JQ_start_tool");
        var $start_pressure = $("#JQ_start_pressure");
        var $start_full = $("#JQ_start_full");
        var $begin_recordMan = $("#JQ_start_recordMan");
        var $start_remark = $("#JQ_start_remark");

        $start_day.text(startBall.lastLimitDays);
        $start_ball.text(startBall.pipeValName);
        $start_mile.text(startBall.mileage);
        $start_pile.text(startBall.reference);
        $start_pileMile.text(startBall.referencedistance);
        $start_time.text(startBall.sendBallsTime);
        $start_man.text(startBall.arriveUser);
        $start_out.text(startBall.ballsPressureFrom);
        $start_in.text(startBall.ballsPressureTo);
        $start_flow.text(startBall.ballsFlow);
        $start_bore.text(startBall.interDiameter);
        $start_type.text(startBall.pagType);
        $start_tool.text(startBall.pagTool);
        $start_pressure.text(startBall.pressure);
        $start_full.text(startBall.interference);
        $begin_recordMan.text(startBall.recrder);
        $start_remark.val(startBall.remark);

        $start_day.attr('title',startBall.lastLimitDays);
        $start_ball.attr('title',startBall.pipeValName);
        $start_mile.attr('title',startBall.mileage);
        $start_pile.attr('title',startBall.reference);
        $start_pileMile.attr('title',startBall.referencedistance);
        $start_time.attr('title',startBall.sendBallsTime);
        $start_man.attr('title',startBall.arriveUser);
        $start_out.attr('title',startBall.ballsPressureFrom);
        $start_in.attr('title',startBall.ballsPressureTo);
        $start_flow.attr('title',startBall.ballsFlow);
        $start_bore.attr('title',startBall.interDiameter);
        $start_type.attr('title',startBall.pagType);
        $start_tool.attr('title',startBall.pagTool);
        $start_pressure.attr('title',startBall.pressure);
        $start_full.attr('title',startBall.interference);
        $begin_recordMan.attr('title',startBall.recrder);
        $start_remark.attr('title',startBall.remark);
    }

    //收球点信息
    function drawEndBall(data){
        var endBall = DyyBase.clearNull(data);
        var $end_ball = $("#JQ_end_ball");
        var $end_mile = $("#JQ_end_mile");
        var $end_pile = $("#JQ_end_pile");
        var $end_pileMile = $("#JQ_end_pileMile");
        var $end_time = $("#JQ_end_time");
        var $end_man = $("#JQ_end_man");
        var $end_out = $("#JQ_end_out");
        var $end_in = $("#JQ_end_in");
        var $end_flow = $("#JQ_end_flow");
        var $end_speed = $("#JQ_end_speed");
        var $end_water = $("#JQ_end_water");
        var $end_goods = $("#JQ_end_goods");
        var $end_pressure = $("#JQ_end_pressure");
        var $end_air = $("#JQ_end_air");
        var $end_pipeAir = $("#JQ_end_pipeAir");
        var $end_gas = $("#JQ_end_gas");
        var $end_temper = $("#JQ_end_temper");
        var $end_runMile = $("#JQ_end_runMile");
        var $end_coe = $("#JQ_end_coe");
        var $end_recordMan = $("#JQ_end_recordMan");
        var $end_cleanTime = $("#JQ_end_cleanTime");
        var $end_ratio = $("#JQ_end_ratio");
        var $end_pull = $("#JQ_end_pull");
        var $end_toolDetail = $("#JQ_end_toolDetail");
        var $end_remark = $("#JQ_end_remark");

        $end_ball.attr('title',endBall.pipeValName);
        $end_mile.attr('title',endBall.mileage);
        $end_pile.attr('title',endBall.reference);
        $end_pileMile.attr('title',endBall.referencedistance);
        $end_time.attr('title',endBall.backBallsTime);
        $end_man.attr('title',endBall.arriveUser);
        $end_out.attr('title',endBall.ballsPressureFrom);
        $end_in.attr('title',endBall.ballsPressureTo);
        $end_flow.attr('title',endBall.ballsFlow);
        $end_speed.attr('title',endBall.avgRate);
        $end_water.attr('title',endBall.sewage);
        $end_goods.attr('title',endBall.dirt);
        $end_pressure.attr('title',endBall.maxPushPressure);
        $end_air.attr('title',endBall.ventGasVolume);
        $end_pipeAir.attr('title',endBall.actualPassGasVolume);
        $end_gas.attr('title',endBall.gasRelativedEnsity);
        $end_temper.attr('title',endBall.avgTempreture);
        $end_runMile.attr('title',endBall.pigRunLength);
        $end_coe.attr('title',endBall.compressionCoefficient);
        $end_recordMan.attr('title',endBall.recrder);
        $end_cleanTime.attr('title',endBall.pagDate);
        $end_ratio.attr('title',endBall.avgSpeed);
        $end_pull.attr('title',endBall.interference);
        $end_toolDetail.attr('title',endBall.pigDescribe);
        $end_remark.attr('title',endBall.dirtdescRiption);

        $end_ball.text(endBall.pipeValName);
        $end_mile.text(endBall.mileage);
        $end_pile.text(endBall.reference);
        $end_pileMile.text(endBall.referencedistance);
        $end_time.text(endBall.backBallsTime);
        $end_man.text(endBall.arriveUser);
        $end_out.text(endBall.ballsPressureFrom);
        $end_in.text(endBall.ballsPressureTo);
        $end_flow.text(endBall.ballsFlow);
        $end_speed.text(endBall.avgRate);
        $end_water.text(endBall.sewage);
        $end_goods.text(endBall.dirt);
        $end_pressure.text(endBall.maxPushPressure);
        $end_air.text(endBall.ventGasVolume);
        $end_pipeAir.text(endBall.actualPassGasVolume);
        $end_gas.text(endBall.gasRelativedEnsity);
        $end_temper.text(endBall.avgTempreture);
        $end_runMile.text(endBall.pigRunLength);
        $end_coe.text(endBall.compressionCoefficient);
        $end_recordMan.text(endBall.recrder);
        $end_cleanTime.text(endBall.pagDate);
        $end_ratio.text(endBall.avgSpeed);
        $end_pull.text(endBall.interference);
        $end_toolDetail.text(endBall.pigDescribe);
        $end_remark.val(endBall.dirtdescRiption);
    }

    //**表格数据**
    function drawTable(data){
        DTTABLE = $table.DataTable({
            'language': {
                'zeroRecords': DyyBase.table_empty()
            },
            'info': false,
            'paging': false,
            'autoWidth': false,
            'order': [],
            'serverSide': false,
            'pageLength': 1000,
            'ordering': false,
            'processing': false,
            'searching': false,
            'lengthChange': false,
            'data': data,
            'columns': [
                {
                    data: 'locationDescription',
                    render: function (data) {
                        return '<span title="' + data + '">' + data + '</span>';
                    }
                },
                {
                    data: 'mileage',
                    render: function (data) {
                        return '<span title="' + data + '">' + data + '</span>';
                    }
                },
                {
                    data: 'avgSpeed',
                    render: function (data) {
                        return '<span title="' + data + '">' + data + '</span>';
                    }
                },
                {
                    data: 'passTime',
                    render: function (data) {
                        return '<span title="' + data + '">' + data + '</span>';
                    }
                },
                {
                    data: 'recrder',
                    render: function (data) {
                        return '<span title="' + data + '">' + data + '</span>';
                    }
                },
                {
                    data: 'createRemark',
                    render: function (data) {
                        return '<span title="' + data + '">' + data + '</span>';
                    }
                },
                {
                    data: 'id',
                    render: function (data) {
                        return '<i data-a="' + data + '" class="xyf-see-image-icon" title="查看图片"></i>';
                    }
                }
            ]
        });
    }

    // 已被转化的图片数量
    var count = 0;
    // 作业点图片总数量
    var work_pic = [];
    // 将图片临时存储
    window.storeImage = function (img) {
        work_pic.push(img);
    };

    // 导出pdf操作
    function exportPDF() {
        var pipeName = taskInfo.lineName;
        var projectName = taskInfo.taskName;
        DyyBase.isLogin(function () {
            $(this).EXPT({
                type: 'name',
                name: pipeName + projectName + '清管记录',
                yesBtnAct: function (callback, fname) {
                    callback();
                    canvasPdf.exportHtmlPdf({
                        dom: $('#JQ_history_detail'),
                        server: DyyBase.root_path + '/comm/downImgPdf.json',
                        param: {
                            fileName: fname,
                            name:  projectName + '清管记录'
                        }
                    });
                }
            });
        });
    }

    // 将图片base64转码
    function imgBase64(pics) {
        if (pics[count]) {
            try {
                var canvas = document.createElement('canvas');
                canvas.width = 110;
                canvas.height = 95;
                var ctx = canvas.getContext('2d');
                ctx.drawImage(pics[count], 0, 0, 110, 95);
                var ext = pics[count].src.substring(pics[count].src.lastIndexOf('.') + 1).toLowerCase();
                var dataURL = canvas.toDataURL('image/' + ext);
                pics[count].src = dataURL;
                pics[count].removeAttribute('onload');
                pics[count].removeAttribute('crossorigin');
                count++;
                if (pics[count]) {
                    imgBase64(pics);
                } else {
                    if (count === work_pic.length) {
                        exportPDF();
                    }
                }
            } catch (e) {
                throw new Error(e.message);
            }
        }
    }

    // 导出pdf
    $('#JQ_export_pdf').click(function () {

        if (count == 0 && work_pic.length > 0) {
            imgBase64(work_pic);
        } else {
            exportPDF();
        }

    });

    //表格查看图片
    $table.on("click","i.xyf-see-image-icon",function(){
        var $this = $(this);
        var id = $this.attr('data-a');
        DyyBase.ajax(cleanPipeApi.url_clean_trackDetail_img(),{businessId:id}, function(dataset){
            if(dataset.bool){
                if(dataset.result.length >0){
                    var data = DyyBase.getImagePath(dataset.result);
                    $('#JQ_img_div').loadImg(data,'');
                }else{
                    $this.MSG({'content':'该跟踪点没有上传图片！'});
                }
            }
        });
    });
})(jQuery,window,document);